using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GameStateManager;

namespace ScreenTest.Screens
{
    class MainMenuScreen : MenuScreen
    {
        float scaleOfChoice = 1f;
        bool scaleUp = true;
        const float SCALE_FACTOR = 0.03f;

        public MainMenuScreen() : base()
        {
            menuItems.Add("New Game");
            menuItems.Add("Controls");
            menuItems.Add("Exit");
        }

        public override void HandleInput(InputState inputState, GameTime gameTime)
        {
            if (inputState.MenuItemSelect)
            {
                ReactToMenuChoice();
                
            }

            if (inputState.MenuItemDown)
            {
                currentChoice++;
                if (currentChoice > 2)
                    currentChoice = 0;
            }

            if (inputState.MenuItemUp)
            {
                currentChoice--;
                if (currentChoice < 0)
                {
                    currentChoice = 2;
                }
            }


        }

        void ReactToMenuChoice()
        {
            if (currentChoice == 0)
            {
                LoadingScreen.Load(ScreenManager, this.LoadNextScreen);
            }
            else if (currentChoice == 1)
            {
                //PResent the controls here
            }
            else if (currentChoice == 2)
            {
                ScreenManager.Game.Exit();
            }
            
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus)
        {
            if (scaleUp)
            {
                scaleOfChoice += SCALE_FACTOR;
                if (scaleOfChoice > 2f)
                {
                    scaleUp = false;
                }
            }
            else
            {
                scaleOfChoice -= SCALE_FACTOR;

                if (scaleOfChoice < 1.0f)
                {
                    scaleUp = true;
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.GraphicsDevice.Clear(Color.Black);

            int spacing = menuFont.LineSpacing;
            Vector2 position = new Vector2(100, 100);

            ScreenManager.SpriteBatch.Begin();
            for (int i=0; i < menuItems.Count; i++)
            {
                String item = menuItems[i];

                if (i == currentChoice)
                {
                    ScreenManager.SpriteBatch.DrawString(menuFont, item, position, Color.White, 0f, Vector2.Zero, scaleOfChoice, SpriteEffects.None, 0f);
                }
                else
                {
                    ScreenManager.SpriteBatch.DrawString(menuFont, item, position, Color.White);
                }
                position.Y += spacing;
            }
            ScreenManager.SpriteBatch.End();
        }

        public void LoadNextScreen(object sender, EventArgs args)
        {
            ScreenManager.AddScreen(new GamePlayScreen());
        }
    }
}
